package ca.venasse.pinventory.inventory;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import org.axonframework.eventhandling.annotation.EventHandler;
import org.axonframework.eventsourcing.annotation.AbstractAnnotatedEntity;

import ca.venasse.pinventory.inventory.command.CreateChildLocation;
import ca.venasse.pinventory.inventory.common.LocationIdentifier;
import ca.venasse.pinventory.inventory.common.LocationName;

public class Location extends AbstractAnnotatedEntity {

	private final List<LocationIdentifier> childLocations;
	private final LocationIdentifier identifier;
	private final LocationName name;

	public Location(LocationIdentifier identifier, LocationName name) {
		this.identifier = identifier;
		this.name = name;
		this.childLocations = newArrayList();
	}

	public LocationName getName() {
		return this.name;
	}

	@EventHandler
	protected void handle(CreateChildLocation event) {
		if (!this.identifier.equals(event.getParentLocationIdentifier())) {
			return;
		}

		this.childLocations.add(event.getLocationIdentifier());
	}

}
